package cn.itcast.user.web;

import cn.itcast.user.pojo.User;
import cn.itcast.user.pojo.YmlConfig;
import cn.itcast.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Value("${server.port}")
    String port;
    @Autowired
    private UserService userService;

    //@Value("${pattern.dateformat}")
    //private String dateformat;

    @Autowired
    private YmlConfig ymlConfig;

    @GetMapping("now")
    public String now() {
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(ymlConfig.getDateformat()));
    }

    @GetMapping("prop")
    public String prop() {
        return ymlConfig.getIntroduce();
    }

    /**
     * 路径： /user/110
     *
     * @param id 用户id
     * @return 用户
     */
    @GetMapping("/{id}")
    public User queryById(@PathVariable("id") Long id, @RequestHeader(value = "name", required = false) String name) {

        System.out.println("请求头中的name " + name);

        User user = userService.queryById(id);
        user.setAddress(port);
        return user;
    }
}
